package com.wokeMy.view.channe.Jiupai;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class JiupaiActivity extends BaseActivity implements View.OnClickListener {

    TextView money_jpKjzf_tv,zfcard_jpKjzf,jscard_jpKjzf;
    RelativeLayout zhifu_jpKjzf_rl;
    RelativeLayout jieshuan_jpKjzf_rl;
    ImageView back;
    Button paybt;

    Data_bindcarlist listdata,payListdata;
    Intent intent;
    float money;
    int way_id,vip_level;
    String user_id;
    String pay_card_no,card_no;
    UnionPayChannel unionPayChannel;
    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_jiuai);
    }

    @Override
    public void initActivity() {
        zhifu_jpKjzf_rl.setOnClickListener(this);
        jieshuan_jpKjzf_rl.setOnClickListener(this);
        back.setOnClickListener(this);
        paybt.setOnClickListener(this);
        money_jpKjzf_tv.setText("￥"+money);
    }

    @Override
    public void initView() {
        money_jpKjzf_tv = myFindViewById(R.id.money_jpKjzf_tv);
        zhifu_jpKjzf_rl = myFindViewById(R.id.zhifu_jpKjzf_rl);
        jieshuan_jpKjzf_rl = myFindViewById(R.id.jieshuan_jpKjzf_rl);
        zfcard_jpKjzf = myFindViewById(R.id.zfcard_jpKjzf);
        jscard_jpKjzf = myFindViewById(R.id.jscard_jpKjzf);
        back = myFindViewById(R.id.back_jpKjzf_iv);
        paybt = myFindViewById(R.id.pay_jpKjzf_bt);
    }

    @Override
    public void initData() {
        intent=getIntent();
        money = intent.getFloatExtra ("money", 0.01f);
        way_id = intent.getIntExtra ("way_id", 0);
        //        user_id = intent.getStringExtra("user_id");
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        vip_level=(Integer.parseInt(((MyApp)getApplication()).getDatas_load().getUser_type()));
        //        unionPayChannel=(UnionPayChannel)intent.getSerializableExtra("unionPayChanne");
        //        Log.e("initData",money+" "+unionPayChannel.getId()+" "+unionPayChannel.get)
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.money_jpKjzf_tv:
                break;
            case R.id.back_jpKjzf_iv:
                finish();
                break;
            case R.id.pay_jpKjzf_bt:
                if (pay_card_no==null||pay_card_no.equals("")){
                    Toast.makeText(JiupaiActivity.this,"支付卡未选择",Toast.LENGTH_SHORT).show();
                }else if(card_no==null||card_no.equals("")){
                    Toast.makeText(JiupaiActivity.this,"结算卡未选择",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent=new Intent(JiupaiActivity.this,JiupaikjzfActivity.class);
                    intent.putExtra("paylistdata",payListdata);
                    intent.putExtra("listdata",listdata);
                    intent.putExtra ("money",money);
                    intent.putExtra ("way_id",way_id);
                    startActivity(intent);
                }
                //                Toast.makeText(JiupaiActivity.this,"暂未开通",Toast.LENGTH_SHORT).show();
                break;
            case R.id.zhifu_jpKjzf_rl:
                Intent intent = new Intent (JiupaiActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "zhifucard");
                startActivityForResult (intent, Constant.REQUEST_ZHIFU);
                break;
            case R.id.jieshuan_jpKjzf_rl:
                Intent intent2 = new Intent (JiupaiActivity.this, MybankblindActivity.class);
                intent2.putExtra ("czhiortxian", "zscard");
                startActivityForResult (intent2, Constant.REQUEST_JIESHUAN);
                break;
            default:
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //        Log.e("RESPONSE_JIESHUAN",resultCode+"");
        if (resultCode==Constant.RESPONSE_ZHIFU){
            payListdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=payListdata.cardno;
            zfcard_jpKjzf.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        }else if(resultCode==Constant.RESPONSE_JIESHUAN){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            card_no=listdata.cardno;
            jscard_jpKjzf.setText(card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+card_no.substring(card_no.length()-3,card_no.length()));
        }
    }

    //九派银联绑卡(收款卡)
    private void unBindCard() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("card_no",card_no);
        Log.e("九派银联绑卡(收款卡)params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.UNJPBIBDCARD_URL, params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                        try {
                            String result = new String(bytes, "utf-8");
                            //                        Toast.makeText(JiupaiActivity.this, result, Toast.LENGTH_SHORT).show();
                            Log.e("九派银联绑卡(收款卡)result", result);
                            try {
                                JSONObject jsonObject = new JSONObject(result);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //九派银联解绑(收款卡)
    private void bindCard() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("card_no",card_no);
        Log.e("九派银联解绑(收款卡)params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.JPBIBDCARD_URL, params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                        try {
                            String result = new String(bytes, "utf-8");
                            //                        Toast.makeText(JiupaiActivity.this, result, Toast.LENGTH_SHORT).show();
                            Log.e("九派银联解绑(收款卡)result", result);
                            try {
                                JSONObject jsonObject = new JSONObject(result);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //九派短信验证下单
    private void yemXd() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("credit_card",pay_card_no); //付款卡（信用卡）
        params.put("debit_card",card_no);//收款卡 （储蓄卡）
        params.put("amount",money);//收款卡 （储蓄卡）
        Log.e("九派短信验证下单params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.JPYZMXD_URL, params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                        try {
                            String result = new String(bytes, "utf-8");
                            //                        Toast.makeText(JiupaiActivity.this, result, Toast.LENGTH_SHORT).show();
                            Log.e("九派短信验证下单result", result);
                            try {
                                JSONObject jsonObject = new JSONObject(result);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //九派支付提交
    private void jpZf() {
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("credit_card",pay_card_no); //付款卡（信用卡）
        params.put("debit_card",card_no);//收款卡 （储蓄卡）
        params.put("amount",money);
        params.put("checkCode","0000");  //验证码
        Log.e("九派支付提交params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.ZFJP_URL, params, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                //判断状态码
                if(i == 200){
                    //获取结果
                        try {
                            String result = new String(bytes, "utf-8");
                            //                        Toast.makeText(JiupaiActivity.this, result, Toast.LENGTH_SHORT).show();
                            Log.e("九派支付提交result", result);
                            try {
                                JSONObject jsonObject = new JSONObject(result);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
}
